package com.xiaolaoyiguan.controller;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;


import com.xiaolaoyiguan.entity.Article;
import com.xiaolaoyiguan.service.HomeService;


/**
 * controller
 * @author Administrator
 *
 */


@Controller
@RequestMapping("/article")
public class HomeController {
    private final  Logger logger=Logger.getLogger(HomeController.class);
    Map<String,Object> map=new HashMap<String,Object>();
    
    @Resource
    private HomeService homeService;
    
    /**
     * list
     * @param model 封装返回返回数据list
     * @return 前台接收页面
     */
    @RequestMapping(value={"","/list"})
    public String getAll (Model model) {
    	List<Article> list = homeService.getAll();
    	model.addAttribute("list", list);
    	return "/main_list";
    }
    
    
    /**
     * 新增
     * @param art entity对象
     * @param bindingResult  验证的错误返回
     * @param attr  重定向
     * @return  前台jsp页面
     */
    @RequestMapping("/add")
    public String addArticle(
				    		@Valid Article art,
    		                BindingResult bindingResult,
    		                RedirectAttributes attr) {

    	homeService.add(art);
    	map.put("msg", "succesful!");
    	return "redirect:/article/list";
    }
    
    /**
     * 修改
     * @param article_title  接收参数文章标题
     * @param article_content  文章内容
     * @param edit_date  文章修改日期（项目未使用）
     * @param article_author 作者
     * @param article_category  文章类别
     * @param id  文章ID
     * @param create_date  文章创建日期
     * @return  前台接收页面
     */
    @RequestMapping("/edit")
    public String editArticle(String article_title,
    		                  String article_content,
    		                  String edit_date,
    		                  String article_author,
    		                  String article_category,
    		                  String id,
    		                  String create_date
    		                  ) {
    	Article art=new Article();
    	art.setId(id);
    	art.setCreate_date(create_date);
    	art.setArticle_category(article_category);
    	art.setArticle_title(article_title);
    	art.setArticle_author(article_author);
    	art.setArticle_content(article_content);
    	art.setEdit_date(edit_date);
    	homeService.edit(art);
    	map.put("msg", "successful!");
    	return "redirect:/article/list";
    }
    
    /**
     * 新增修改跳转页面
     * @param id 文章ID
     * @param model 封装返回数据
     * @return  返回前台页面
     */
    @RequestMapping("/add_edit")
    public String addEditArticle(String id,Model model){
    	Article art = null;
    	if("".equals(id)||id==null) {
    	    art=new Article();
    		model.addAttribute("msg", "add");
    	}else {
    		model.addAttribute("msg", "edit");
    		art = (Article) homeService.findAtricleById(id);
    	}
    	model.addAttribute("id", id);
    	model.addAttribute("article", art);
		return "/main_edit";
    }
    
    /**
     *  文章详细页面
     * @param id 文章ID
     * @param model 封装返回数据
     * @return 返回详情页
     */
    @RequestMapping("/article_details")
    public String detailsArticle(String id,Model model){
    	Article art = (Article) homeService.findAtricleById(id);
    	model.addAttribute("article", art);
    	return "/main_details";
    }
}
